import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:xin_nuo_pda/bean/bin_info_entity.dart';
import 'package:xin_nuo_pda/bean/edc_data_entity.dart';
import 'package:xin_nuo_pda/routes/Routes.dart';
import 'package:xin_nuo_pda/utils/string_extension.dart';
import 'package:xin_nuo_pda/values/colors.dart';
import 'package:xin_nuo_pda/values/constant.dart';
import 'package:xin_nuo_pda/views/SubTitleBar.dart';
import 'package:xin_nuo_pda/views/TextView.dart';
import 'package:xin_nuo_pda/views/edittext/TitleEditText.dart';

import 'PassEndWorkController.dart';

class PassEndWorkPage extends StatelessWidget {
  final controller = Get.put(PassEndWorkController());
  final state = Get.find<PassEndWorkController>().state;
  TextEditingController textController = TextEditingController();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: Text(USER),
        ),
        body: SingleChildScrollView(
          child: GetBuilder<PassEndWorkController>(
              builder: (controller) => Container(
                    child: Column(
                      children: [
                        SubTitleBar("良品出站", '下一步', () {
                          if(state.binDataEntity == null || state.binData == null){
                            Get.snackbar('提示', '未获取到BIN数据');
                            return;
                          }

                          controller.next();
                        }),
                        TitleEditText(
                          "批次ID:",
                          "请扫描批次ID",
                          (value) {
                              controller.getLotInfo(value);
                          },
                          controller: textController,
                        ),
                        TextView("设备ID(主):",  state.lotEntity?.eQUIPMENTID ??''),
                        TextView("批次数量:", state.lotEntity?.mAINQTY.toString() ??'0'),
                        Column(
                          children: [
                            Container(
                              margin: EdgeInsets.all(8),
                              alignment: Alignment.centerLeft,
                              child: Text(
                                "BIN信息",
                                style: TextStyle(
                                    fontWeight: FontWeight.bold, fontSize: 16),
                              ),
                            ),
                            GetBuilder<PassEndWorkController>(
                              builder: (controller) => Table(
                                defaultVerticalAlignment:
                                    TableCellVerticalAlignment.middle,
                                columnWidths: {
                                  //列宽
                                  0: FlexColumnWidth(1.0),
                                  1: FlexColumnWidth(1.0),
                                  2: FlexColumnWidth(1.0),
                                  3: FlexColumnWidth(1.0),
                                  // 4: FlexColumnWidth(1.0),
                                },
                                border: TableBorder.all(
                                  color: Colors.grey,
                                  width: 1.0,
                                  style: BorderStyle.solid,
                                ),
                                children: getTableList(context),
                              ),
                              id: state.passEndWorkBinSelectId,
                            ),
                          ],
                        )
                      ],
                    ),
                  ),
              id: state.passEndWorkId),
        ));
  }

  List<TableRow> getTableList(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                'BIN名称',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          // Align(
          //   alignment: Alignment.center,
          //   child: Text(
          //     'BIN名称',
          //     style: TextStyle(
          //       color: theme_blue.toColor(),
          //     ),
          //   ),
          // ),
          Align(
            alignment: Alignment.center,
            child: Text(
              'BIN 描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '占比良率',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    if (state.binData == null) {
      state.binData = [];
    }
    for (int i = 0; i < state.binData!.length; i++) {
      BinInfo element = state.binData![i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.BINNAME ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        // Align(
        //   alignment: Alignment.center,
        //   child: Text(
        //     element.BINNAME ?? '',
        //     style: TextStyle(
        //       color: text_black.toColor(),
        //     ),
        //   ),
        // ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.BINDESC ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.MAINQTY.toString(),
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
              element.MAINQTY.toString().isEmpty ? '0%' :  ((element.MAINQTY!.toInt() /
                (state.lotEntity!.mAINQTY ?? 0)) * 100).toStringAsFixed(2).toString() + '%',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
      ]));
    }
    return tempView;
  }

  List<TableRow> getTableList2(BuildContext context) {
    List<TableRow> tempView = [];
    List<FocusNode> focusNodeList = [];
    tempView.add(TableRow(
        children: [
          //增加行高
          SizedBox(
            height: 40,
            child: Align(
              alignment: Alignment.center,
              child: Text(
                'BIN名称',
                style: TextStyle(
                  color: theme_blue.toColor(),
                ),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              'BIN 描述',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '数量',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
          Align(
            alignment: Alignment.center,
            child: Text(
              '占比良率',
              style: TextStyle(
                color: theme_blue.toColor(),
              ),
            ),
          ),
        ],
        //第一行样式 添加背景色
        decoration: BoxDecoration(
          color: table_head.toColor(),
        )));
    for (int i = 0; i < state.binData!.length; i++) {
      BinInfo element = state.binData![i];
      focusNodeList.add(FocusNode());
      tempView.add(TableRow(children: [
        //增加行高
        SizedBox(
          height: 40,
          child: Align(
            alignment: Alignment.center,
            child: Text(
              element.BINNAME ?? '',
              style: TextStyle(
                color: text_black.toColor(),
              ),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.BINDESC ?? '',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.MAINQTY.toString(),
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
        Align(
          alignment: Alignment.center,
          child: Text(
            element.MAINQTY.toString().isEmpty ? '0%' :  ((element.MAINQTY!.toInt() / (state.lotEntity!.mAINQTY ?? 0)) * 100).toString() + '%',
            style: TextStyle(
              color: text_black.toColor(),
            ),
          ),
        ),
      ]));
    }
    return tempView;
  }
}
